package cn.yuxiaoyuan.service.impl;

import cn.yuxiaoyuan.article.doamin.Article;
import cn.yuxiaoyuan.article.doamin.BlogArticle;
import cn.yuxiaoyuan.article.query.ArticleQuery;
import cn.yuxiaoyuan.common.util.R;
import cn.yuxiaoyuan.mapper.ArticleMapper;
import cn.yuxiaoyuan.service.IArticleService;
import cn.yuxiaoyuan.service.base.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author solargen
 * @version 1.0
 * @date 2025/5/13 10:39
 */
@Service
public class ArticleServiceImpl extends BaseServiceImpl<Article> implements IArticleService {
   @Autowired
   private ArticleMapper articleMapper;
   @Autowired
   private MongoTemplate mongoTemplate;

    @Override
    public void add(Article article) {
//        article.setPublishId();//当前发布人  todo
        article.setCreateTime(new Date());
        article.setArticleReadCount(0);
        article.setArticleStarNum(0);
        article.setArticleCommentNum(0);
        articleMapper.add(article);

        BlogArticle blogArticle = new BlogArticle();
        BeanUtils.copyProperties(article,blogArticle);//类复制  条件  字段和类型必须一致
        mongoTemplate.save(blogArticle);
    }

    @Override
    public void update(Article article) {
        article.setUpdateTime(new Date());
        articleMapper.update(article);

        Query query = new Query(Criteria.where("id").is(article.getId()));
        BlogArticle one = mongoTemplate.findOne(query, BlogArticle.class);
        BeanUtils.copyProperties(article,one);//类复制  条件  字段和类型必须一致  不一致的则不管
        mongoTemplate.save(one);
    }

    @Override
    public void deleteById(Long id) {
        articleMapper.deleteById(id);
        Query query = new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query, BlogArticle.class);
    }

    @Override
    public R pageList(ArticleQuery article) {
        //先走mongodb
        // 如果没有数据 在查询数据库
        PageHelper.startPage(article.getPage(),article.getSize());
        return R.ok(new PageInfo<>(articleMapper.pageList(article)));
    }

    @Override
    public Map<String, List<Article>> place() {
        //排好序的结果
        List<Article> orderByCreate = articleMapper.findOrderByCreate();
        //要返回的数据
        Map<String,List<Article>> map = new HashMap<>();
        //对结果进行处理
        for (Article article : orderByCreate) {
            // 获取年份
            Date createTime = article.getCreateTime();
            //得到时间
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(createTime);
            //将年份转为字符串
            String year = String.valueOf(calendar.get(Calendar.YEAR));
            /**
             * 判断 当前年在map中是否存在
             *      如果不存在 则将年当key存入map中
             *      如果存在 则将对应年份取 当前数据插入到后面
             */
            if(map.get(year) == null){
                List<Article> list = new ArrayList<>();
                list.add(article);
                map.put(year,list);
            }else {
                List<Article> articles = map.get(year);
                articles.add(article);
            }
        }
        return map;
    }

    @Override
    public void synchronousDataToMongodb() {
        List<Article> all = articleMapper.findAll();
        List<BlogArticle> blogArticleList = all.stream()
                .map(article -> {
                    BlogArticle blogArticle = new BlogArticle();
                    BeanUtils.copyProperties(article, blogArticle);
                    return blogArticle;
                }).collect(Collectors.toList());
        mongoTemplate.dropCollection("article");
        mongoTemplate.insert(blogArticleList);
    }

}
